package psn.kiko.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import psn.kiko.mapper.UserMapper;
import psn.kiko.pojo.PageDTO;
import psn.kiko.pojo.PageR;
import psn.kiko.pojo.R;
import psn.kiko.pojo.dto.UserDTO;
import psn.kiko.pojo.entity.User;
import psn.kiko.pojo.vo.UserVO;

import java.util.List;

/**
 * 2024/11/10 13:10
 */
@Service
@RequiredArgsConstructor
public class UserService{
	private final UserMapper userMapper;
	
	public R<?> addUser(UserDTO dto){
		User user = new User();
		BeanUtils.copyProperties(dto,user);
		Integer r = userMapper.insertOne(user);
		return R.okWithData(r);
	}
	
	public R<?> getUserById(Integer id){
		UserVO r = userMapper.selectOneById(id);
		return R.okWithData(r);
	}
	
	public R<?> deleteUserById(Integer id){
		Integer r = userMapper.deleteById(id);
		return R.okWithData(r);
	}
	
	public R<?> updateUserById(UserDTO dto){
		User user = new User();
		BeanUtils.copyProperties(dto,user);
		Integer r = userMapper.updateById(user);
		return R.okWithData(r);
	}
	
	public R<?> pageUser(PageDTO<UserDTO> pageDto){
		Page<UserVO> page = PageHelper.startPage(pageDto.getPageNum(),pageDto.getPageSize());
		User user = new User();
		BeanUtils.copyProperties(pageDto.getCond(),user);
		userMapper.selectUsers(user);
		long total = page.getTotal();
		List<UserVO> users = page.getResult();
		PageR<Object> r = PageR.builder()
				.total(total)
				.data(users).build();
		return R.okWithData(r);
	}
}
